package com.example.mapper;

import com.example.entity.User;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;

import java.util.List;

public interface UserMapper {
    @Select("SELECT * FROM users")
    List<User> getAllUsers();

    @SelectProvider(type = UserSqlProvider.class, method = "getUsersByCondition")
    List<User> getUsersByCondition(@Param("user") User user);

    @Select("SELECT u.id, u.username, u.age, r.id as role_id, r.role_name " +
            "FROM users u " +
            "JOIN user_roles ur ON u.id = ur.user_id " +
            "JOIN roles r ON ur.role_id = r.id")
    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "username", column = "username"),
            @Result(property = "age", column = "age"),
            @Result(property = "role", column = "role_id",
                    one = @One(select = "com.example.mapper.RoleMapper.getRoleById", fetchType = FetchType.EAGER))
    })
    List<User> getUsersWithRoles();
}